package com.wb3.meta.common.handler;

import com.wb3.meta.common.exceptions.AuthException;
import com.wb3.meta.common.exceptions.ImportException;
import com.wb3.meta.common.pojo.Result;
import com.wb3.meta.common.pojo.ResultT;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.naming.AuthenticationException;
import java.util.HashMap;
import java.util.Map;


/**
 * 自定义异常处理类
 * 针对不同的异常自定义不同的方法
 * 环绕通知
 * 切面:针对所有的controller中抛出的异常
 * 若使用@ControllerAdvice,则不会自动转换为JSON格式
 */
@RestControllerAdvice
@Slf4j
public class RestExceptionHandler {

    /**
     * IllegalArgumentException异常处理返回json
     * 状态码:400
     * @param exception
     * @return
     */
    @ExceptionHandler({ IllegalArgumentException.class })
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Map<String, Object> badRequestException(IllegalArgumentException exception) {
        Map<String, Object> data = new HashMap<>();
        data.put("code", HttpStatus.BAD_REQUEST.value());
        data.put("msg", exception.getMessage());
        return data;
    }

    /**
     * 权限异常拦截
     * @param exception
     * @return
     */
    @ExceptionHandler({ AuthenticationException.class })
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public Map<String, Object> badRequestException(AuthenticationException exception) {
        Map<String, Object> data = new HashMap<>();
        data.put("code", HttpStatus.UNAUTHORIZED.value());
        data.put("msg", exception.getMessage());
        return data;
    }
    /**
     * 服务器异常捕获
     * @param exception
     * @return
     */
    @ExceptionHandler({ Exception.class })
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Map<String, Object> badRequestException(Exception exception) {
        exception.printStackTrace();
        Map<String, Object> data = new HashMap<>();
        data.put("code", HttpStatus.INTERNAL_SERVER_ERROR);
        data.put("msg", exception.getMessage());
        return data;
    }

    /**
     * 数据库异常捕获（避免sql抛出）
     * @param exception
     * @return
     */
    @ExceptionHandler({ DataAccessException.class })
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Map<String, Object> dataAccessException(Exception exception) {
        exception.printStackTrace();
        Map<String, Object> data = new HashMap<>();
        data.put("code", HttpStatus.INTERNAL_SERVER_ERROR);
        data.put("msg", "system error");
        return data;
    }

    /**
     * 运行时异常捕获
     * @param exception
     * @return
     */
    @ExceptionHandler({ RuntimeException.class })
    public Map<String, Object> badRequestException(RuntimeException exception) {
        log.debug("运行时异常");
        exception.printStackTrace();
        Map<String, Object> data = new HashMap<>();
        data.put("code", 0);
        data.put("msg", exception.getMessage());
        return data;
    }


    /**
     * 业务异常处理，导入发生异常，提示错误信息，另外返回错误反馈表下载链接
     * @param e
     * @return ErrorInfo
     */
    @ExceptionHandler({ImportException.class})
    public ResponseEntity<Result> businessExceptionHandler(ImportException e)  {
        return new ResponseEntity(Result.fail(e.getMessage(),e.getFilePath()), HttpStatus.BAD_REQUEST);
    }


    /**
     * 权限异常
     * @param
     * @return ErrorInfo
     */
    @ExceptionHandler(AuthException.class)
    public ResponseEntity<ResultT> handleAuthException(AuthException ex) {
        return new ResponseEntity(ResultT.fail(ex.getCode(),ex.getMsg()),HttpStatus.OK);
    }

}
